package com.h3c.ptability.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.h3c.ptability.bo.GetUserByDeptAndRoleBO;
import com.h3c.ptability.dto.eos.EosRoleClientDTO;
import com.h3c.ptability.dto.eos.EosUserClientDTO;
import com.h3c.ptability.dto.eos.UserRoleDTO;
import com.h3c.ptability.service.EosAuthService;
import com.h3c.ptability.support.ResultData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author chenyifan
 * @date 2022/8/26 14:15
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/eos")
public class EosPartController {

    @Autowired
    EosAuthService eosAuthService;

    /**
     * 获取租户角色列表
     *
     * @return
     */
    @RequestMapping(value = "/getRoleList", method = RequestMethod.GET)
    public ResultData<PageInfo> getRoleList(@RequestParam(name = "query", required = false) String query,
                                            @RequestParam(name = "appId") String appId,
                                            @RequestParam(name = "currentPage") Integer currentPage,
                                            @RequestParam(name = "pageSize") Integer pageSize,
                                            @RequestParam(name = "account", required = false) String account) {
        try {
            Page page = PageHelper.startPage(currentPage, pageSize);
            //执行
            PageInfo<EosRoleClientDTO> pageInfo = page.toPageInfo();
            List<EosRoleClientDTO> roleList = eosAuthService.getRoleList(query, appId, currentPage, pageSize, account);
            pageInfo.setList(roleList);
            return ResultData.data(pageInfo);
        } catch (Exception e) {
            return ResultData.fail(e.getMessage());
        }
    }

    /**
     * 根据roleCode获取对应的用户列表
     *
     * @param roleCode
     * @return
     */
    @RequestMapping(value = "/getUserListByRoleCode", method = RequestMethod.GET)
    public ResultData<PageInfo> getUserListByRoleCode(@RequestParam(name = "roleCode", required = true) String roleCode,
                                                      @RequestParam(name = "appId") String appId,
                                                      @RequestParam(name = "currentPage", required = true) Integer currentPage,
                                                      @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {
            Page page = PageHelper.startPage(currentPage, pageSize);
            List<EosUserClientDTO> userList = eosAuthService.getUserListByRoleCode(roleCode, appId, currentPage, pageSize);
            PageInfo<EosUserClientDTO> pageInfo = page.toPageInfo();
            pageInfo.setList(userList);
            return ResultData.data(pageInfo);
        } catch (Exception e) {
            return ResultData.data(null);
        }
    }

    /**
     * 根据roleCode获取对应的用户列表
     *
     * @param roleCode
     * @return
     */
    @RequestMapping(value = "/getUserListByAppRoleCode", method = RequestMethod.GET)
    public ResultData<PageInfo> getUserListByAppRoleCode(@RequestParam(name = "query") String query,
                                                         @RequestParam(name = "roleCode", required = true) String roleCode,
                                                         @RequestParam(name = "appId") String appId,
                                                         @RequestParam(name = "currentPage", required = true) Integer currentPage,
                                                         @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {
            Page page = PageHelper.startPage(currentPage, pageSize);
            List<EosUserClientDTO> userList = eosAuthService.getUserListByAppRoleCode(roleCode, appId, query, currentPage, pageSize);
            PageInfo<EosUserClientDTO> pageInfo = page.toPageInfo();
            pageInfo.setList(userList);
            return ResultData.data(pageInfo);
        } catch (Exception e) {
            return ResultData.data(null);
        }
    }

    /**
     * 根据角色添加用户
     *
     * @param userRoleDTO
     * @return
     */
    @RequestMapping(value = "/updateUserRole", method = RequestMethod.POST)
    public ResultData<String> updateUserRole(@RequestBody UserRoleDTO userRoleDTO) {
        try {
            eosAuthService.updateUserRole(userRoleDTO);
            return ResultData.success("添加成功");
        } catch (Exception e) {
            log.error("根据角色添加用户失败,原因:{}", e.getMessage());
            return ResultData.fail("添加失败");
        }
    }

    /**
     * 根据角色删除用户
     *
     * @param userRoleDTO
     * @return
     */
    @RequestMapping(value = "/deleteUserRole", method = RequestMethod.POST)
    public ResultData<String> deleteUserRole(@RequestBody UserRoleDTO userRoleDTO) {
        try {
            eosAuthService.deleteUserRole(userRoleDTO);
            return ResultData.success("删除成功");
        } catch (Exception e) {
            log.error("根据角色删除用户失败,原因:{}", e.getMessage());
            return ResultData.fail("删除失败");
        }
    }

    /**
     * 根据账号查询租户中拥有的角色
     *
     * @param account
     * @param appId
     * @return
     */
    @GetMapping("/getUserRoleByAccount")
    public ResultData<List<String>> getUserRoleByAccount(@RequestParam String account, @RequestParam String appId) {
        try {
            return ResultData.data(eosAuthService.getUserRoleByAccount(account, appId));
        } catch (Exception e) {
            return ResultData.fail("查询用户角色失败:{}", e.getMessage());
        }
    }

    /**
     * 获取用户的授权角色
     * @param account 域账号
     * @param appId 租户id
     * @return 用户的授权角色
     */
    @GetMapping("/getRolesByAccount")
    public ResultData<List<EosRoleClientDTO>> getRolesByAccount(@RequestParam String account, @RequestParam String appId) {
        try {
            return ResultData.data(eosAuthService.getRolesByAccount(account, appId));
        } catch (Exception e) {
            return ResultData.fail("查询用户角色失败:{}", e.getMessage());
        }
    }

    /**
     * 根据租户下的角色编码获取对应的用户列表
     *
     * @param roleCode    应用编码
     * @param appId       租户编码
     * @param currentPage 分页,默认1
     * @param pageSize    分页数量，默认10000
     * @return
     */
    @GetMapping("/getAuthUserListByAppRole")
    public ResultData<List<String>> getAuthUserListByAppRole(@RequestParam(required = true) String roleCode,
                                                             @RequestParam(required = true) String appId,
                                                             @RequestParam(defaultValue = "1") Integer currentPage,
                                                             @RequestParam(defaultValue = "10000") Integer pageSize) {
        try {
            List<EosUserClientDTO> userList = eosAuthService.getUserListByAppRoleCode(roleCode, appId, "", currentPage, pageSize);
            if (!CollectionUtils.isEmpty(userList)) {
                List<String> list = userList.stream().map(EosUserClientDTO::getAccount).distinct().collect(Collectors.toList());
                return ResultData.data(list);
            }
            return ResultData.fail("查询角色用户列表失败:未查询到数据");
        } catch (Exception e) {
            return ResultData.fail("查询角色用户列表异常:{}", e.getMessage());
        }
    }

    /**
     * 根据部门和角色查询符合条件的用户
     *
     * @param getUserByDeptAndRoleBO
     * @return
     */
    @PostMapping("/getUserByDeptAndRole")
    public ResultData<Map<String, Map<String, List<String>>>> getUserByDeptAndRole(@RequestBody List<GetUserByDeptAndRoleBO> getUserByDeptAndRoleBO) {
        try {
            return ResultData.data(eosAuthService.getUserByDeptAndRole(getUserByDeptAndRoleBO));
        } catch (Exception e) {
            return ResultData.fail("根据部门和角色查询符合条件的用户失败:{}", e.getMessage());
        }
    }

    /**
     * 根据角色导入用户
     *
     * @param file
     * @return
     */
    @RequestMapping(value = "/importUserRole", method = RequestMethod.POST)
    public ResultData<String> importUserRole(@RequestParam(value = "file") MultipartFile file,
                                             @RequestParam(value = "appId") String appId) {
        try {
            eosAuthService.importUserRole(file, appId);
            return ResultData.success("导入成功");
        } catch (Exception e) {
            return ResultData.fail("根据角色导入用户失败:{}", e.getMessage());
        }
    }

}
